﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Security.Cryptography;

namespace Clipping.Utils
{
   public class ClippingHelper
    {
        /// <summary>
        /// 统计字数
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static int CountWords(string html)
        {
            int chwords = 0;
            int enwords = 0;
            if (string.IsNullOrEmpty(html))
                return 0;
            html = html.Trim();
            html = Regex.Replace(html, "<[^>]*>", "");
            chwords = Regex.Matches(html, @"[^\u0000-\u007f,\s]").Count;
            foreach (Match m in Regex.Matches(html, @"[\u0000-\u007f]*"))
            {
                string tmpstr = m.Value.Trim();
                if (!string.IsNullOrEmpty(tmpstr))
                {
                    tmpstr = string.Format("\t{0}\t", tmpstr);
                    enwords += Regex.Matches(tmpstr, "\\s+").Count - 1;
                }
            }
            return chwords + enwords;
        }
        public static  string Md5Hash(string str_md5_in)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bytes_md5_in = UTF8Encoding.Default.GetBytes(str_md5_in);
            byte[] bytes_md5_out = md5.ComputeHash(bytes_md5_in);
            string str_md5_out = BitConverter.ToString(bytes_md5_out);
            return str_md5_out;
        }
    }
}
